<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            v-model="searchForm.customer_name"
            placeholder="请输入客户姓名"
            prefix-icon="el-icon-user"
            clearable
          />
        </el-col>
        <el-col :span="6">
          <el-select
            v-model="searchForm.service_type"
            placeholder="请选择服务类型"
            clearable
            style="width: 100%"
          >
            <el-option label="维修" value="维修" />
            <el-option label="保养" value="保养" />
            <el-option label="投诉" value="投诉" />
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增服务记录</el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="filteredServices"
        border
        style="width: 100%"
      >
        <el-table-column prop="customer_name" label="客户姓名" width="180" />
        <el-table-column prop="service_type" label="服务类型" width="180" />
        <el-table-column prop="service_record" label="服务记录" />
        <el-table-column label="操作" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="客户姓名" prop="customer_name">
          <el-input v-model="form.customer_name" />
        </el-form-item>
        <el-form-item label="服务类型" prop="service_type">
          <el-select v-model="form.service_type" placeholder="请选择服务类型">
            <el-option label="维修" value="维修" />
            <el-option label="保养" value="保养" />
            <el-option label="投诉" value="投诉" />
          </el-select>
        </el-form-item>
        <el-form-item label="服务记录" prop="service_record">
          <el-input type="textarea" v-model="form.service_record" rows="4" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getServiceList,addService, updateService, deleteService} from '@/api/after/after'
export default {
  name: "saleAfterList",
  data() {
    return {
      loading: false,
      services: [],
      searchForm: {
        customer_name: "",
        service_type: ""
      },
      dialogVisible: false,
      dialogTitle: "",
      form: {
        id: null,
        customer_name: "",
        service_type: "",
        service_record: ""
      },
      rules: {
        customer_name: [{ required: true, message: "请输入客户姓名", trigger: "blur" }],
        service_type: [{ required: true, message: "请选择服务类型", trigger: "change" }],
        service_record: [{ required: true, message: "请输入服务记录", trigger: "blur" }]
      }
    };
  },
  computed: {
    filteredServices() {
      return this.services.filter(service => {
        const customerNameMatch = !this.searchForm.customer_name || 
          service.customer_name.includes(this.searchForm.customer_name);
        const serviceTypeMatch = !this.searchForm.service_type || 
          service.service_type === this.searchForm.service_type;
        return customerNameMatch && serviceTypeMatch;
      });
    }
  },
  mounted() {
    this.fetchServices();
  },
  methods: {
    // 处理搜索
    handleSearch() {
      this.fetchServices();
    },

    // 处理重置
    handleReset() {
      this.searchForm.customer_name = "";
      this.searchForm.service_type = "";
      this.fetchServices();
    },

    // 获取服务列表
    async fetchServices() {
      this.loading = true;
      getServiceList(this.searchForm).then(response => {
        console.log(response)
        this.services = response.data;
      }).catch(err => {
        this.$message.error('获取数据失败');
      }).finally(() => {
        this.loading = false;

      })
    },

    // 处理新增
    handleAdd() {
      this.dialogTitle = "新增服务记录";
      this.form = {
        id: null,
        customer_name: "",
        service_type: "",
        service_record: ""
      };
      this.dialogVisible = true;
    },

    // 处理编辑
    handleEdit(row) {
      this.dialogTitle = "编辑服务记录";
      this.form = { ...row };
      this.dialogVisible = true;
    },

    // 处理删除
    handleDelete(row) {
      this.$confirm('确认删除该记录?', '提示', {
        type: 'warning'
      }).then(async () => {
        let data = {id: row.id}
        deleteService(data).then(res => {
          this.$message.success('删除成功');
          this.fetchServices();
        }).catch(err => {
          this.$message.error('删除失败');
        })
      })
    },

    // 提交表单
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
            if (this.form.id) {
              // 编辑
               updateService(this.form).then(res => {
                 this.$message.success('编辑成功');
                 this.dialogVisible = false;
                 this.fetchServices();
               }).catch(err => {
                 this.$message.error('编辑失败');
               })
            } else {
              // 新增
              addService(this.form).then(res => {
                this.$message.success('保存成功');
                this.dialogVisible = false;
                this.fetchServices();
              }).catch(err => {
                this.$message.error('保存失败');
              })
            }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.app-container {
  padding: 20px;
  
  .search-card {
    margin-bottom: 20px;
  }
  
  .table-card {
    margin-bottom: 20px;
  }
  
  .el-card {
    .el-row {
      margin-bottom: 0;
    }
  }
  
  .el-button {
    margin-left: 10px;
    
    &:first-child {
      margin-left: 0;
    }
  }
}
</style>